/*
 * Copyright 2009 Brian Mackay brianwmackay@msn.com
 *   All Rights Reserved. See end of file
 */
package com.bmackay.vonsteuben.core.hdfs;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class FileSystemTasks {

    private static Configuration conf = new Configuration();
    private static FileSystem fileSystem = null;

    public FileSystemTasks() {
        try {
            conf.setQuietMode(true);
            fileSystem = FileSystem.get(conf);
        } catch (Exception ex) {
            Logger.getLogger(FileSystemTasks.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public  void clean(String path) {
        try {
            Path p = createPath(path);
            if (fileSystem.exists(p) == true) {
                fileSystem.delete(p, true);
            }
        } catch (Exception ex) {
            Logger.getLogger(FileSystemTasks.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public boolean exists(String myPath) {
        boolean b = false;
        try {
            b = fileSystem.exists(createPath(myPath));
        } catch (IOException ex) {
            Logger.getLogger(FileSystemTasks.class.getName()).log(Level.SEVERE, null, ex);
        }
        return b;
    }

    public void delete(String src) throws Exception {
        fileSystem.delete(new Path(src), true);
    }

    public FSDataInputStream open(Path inPath) throws Exception {
        return fileSystem.open(inPath);
    }

    public FSDataOutputStream create(Path outPath) throws Exception {
        return fileSystem.create(outPath);
    }

    public void copyFromLocalFile(String src, String dest) throws Exception {
        fileSystem.copyFromLocalFile(new Path(src), new Path(dest));
    }

    public static Path createPath(String myPath) {
        return new Path(myPath.trim());
    }
}

/*
 * Copyright 2009 Brian Mackay brianwmackay@msn.com
 * All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
